// JavaScript Document
//检验手机号
function BlurMoblie() {
    var str = $('#mobile').val();
    var mbTest = /^(13|14|15|18|17)[0-9]{9}$/;
    if (!mbTest.test(str)) {
        tipsbox("手机格式不正确");
        return false;
    }
    else {
        return true;
    }
}

//检验密码
function BlurPwd() {
    var txt = "#txtPwd";
    var td = "#dvPwd";
    var pat = new RegExp("^.{6,16}$", "i");
    var str = $(txt).val();
    if (!pat.test(str)) {
        tipsbox("密码格式不正确");
        return false;
    }
    else {
        return true;
    }
}

//检验验证码
function BlurCode() {
    var ecode = $("#ecode").val();
    if (ecode.length != 4) {
        tipsbox("请输入4位图形验证码");
        return false;
    }
    else {
        return true;
    }
}

//检验手机验证码
function BlurPhonecode() {
    var phonecode = $("#phonecode").val();
    if (phonecode.length != 6) {
        tipsbox("请输入6位图形验证码");
        return false;
    }
    else {
        return true;
    }
}

function checkecode(e) {
    var str = $(e).val();
    if (str.length == 4) {
        var mbTest = /^(13|14|15|18|17)[0-9]{9}$/;
        if (mbTest.test($('#mobile').val())) {
            $('#btnSendMsg').attr('onclick', 'sendMobileValidSMSCode()').removeClass('dis');
        }
    }
}

function subreg() {
    if (BlurMoblie() == false) return;
    if (BlurCode() == false) return;
    if (BlurPhonecode() == false) return;
    if (BlurPwd() == false) return;
    var mobile = $("#mobile").val();
    var pwd = $("#pwd").val();
    var ecode = $("#ecode").val();
    var phonecode = $("#phonecode").val();
    $.ajax({
        url: "/member/common/m_getnexthtml",
        type: "post",
        dataType: "html",
        data: "dvUser=" + txtuser + "&txtpwd=" + txtpwd,
        success: function (d) {	//返回d数组,status为状态值,message为提示信息。status=1:注册成功;status=0:注册失败;
            if (d.code == 1) {
                $('.mark_bar p').html('注册成功');
                $("#reg_step").hide();
                $("#reg_suc").hide();
            } else {
                tipsbox(d.message);
            }
        }
    });
}

var timer = null;
var leftsecond = 60; //倒计时
var msg = "";

function sendMobileValidSMSCode() {
    var ecode = $("#ecode").val();
    var mobile = $("#mobile").val();
    $('#btnSendMsg').html("短信发送中...");
    $("#btnSendMsg").removeAttr('onclick');
    $.ajax({
        url: "/member/common/sendphonenew/",
        type: "post",
        dataType: "json",
        data: {
            "cellphone": mobile, "ecode": ecode
        },
        success: function (d) {	//返回数组status为状态值，message为提示信息。status=0:发送失败;status=1:发送成功;status=2:手机号码被占用;status=3:图形验证码错误;status=-1:模拟短信信息
            leftsecond = 60;
            if (d.code == 1) {
                tipsbox("发送成功，如未收到,请60秒后重新发送");
                clearInterval(timer);
                timer = setInterval(setLeftTime, 1000, "1");
                $("#btnSendMsg").val("请稍等..");
                $("#mobile").attr("readonly", true);
            } else if (d.code == 2) {
                tipsbox("该手机号码已被其他用户使用！");
                $("#btnSendMsg").attr({
                    onclick: 'sendMobileValidSMSCode()',
                });
                $("#btnSendMsg").html("获取验证码");
                $("#mobile").removeAttr("readonly");
            } else if (d.code == 3) {
                tipsbox("图形验证码不正确，请重新输入！");
                refreshEcode($('.ecode'));
                $("#btnSendMsg").attr({
                    onclick: 'sendMobileValidSMSCode()',
                });
                $("#btnSendMsg").html("获取验证码");
                $("#mobile").removeAttr("readonly");
            }
            else if (d.code == -1) {
                msg = d.message;
                tipsbox(msg);
                clearInterval(timer);
                timer = setInterval(setLeftTime, 1000, "1");
                $("#btnSendMsg").html("请稍等..").addClass('dis');
                $("#mobile").attr("readonly", true);
            } else {
                tipsbox("校验码发送失败,请重试");
                $("#btnSendMsg").html("获取验证码");
                $("#mobile").attr("readonly", true);
            }
        }
    });
}

//刷新重新获取时间
function setLeftTime() {
    var second = Math.floor(leftsecond);
    $("#btnSendMsg").html(second + "s后重新发送");
    leftsecond--;
    if (leftsecond < 1) {
        clearInterval(timer);
        try {
            $("#btnSendMsg").html("获取验证码").removeClass('dis');
            $("#mobile").removeAttr("readonly");
            $("#btnSendMsg").attr({onclick: 'sendMobileValidSMSCode()',});
        } catch (E) {
        }
        return;
    }
}

//刷新验证码
function refreshEcode(e) {
    $(e).prop('src', 'https://www.uximoney.com/?plugins&amp;q=imgcode&amp;t=' + Math.random());
}